<?php

class Vlib_Module_Loader {

    static $_instance = null;

    public function __construct() {
        $this->modules = $this->_getModules();
    }

    /**-----------------------------------------------------------------------------------
     * @desc get a Vlib_Module_Loader instance using singleton pattern
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *-----------------------------------------------------------------------------------*/
    
    public static function getIntance() {
        if (!self::$_instance) self::$_instance = new self();
        return self::$_instance;
    }

    /**-----------------------------------------------------------------------------------
     * @desc get all the avaiable module name (this function is alias of _getModules
     * protected function
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *-----------------------------------------------------------------------------------*/
    
    public function getModules() {
        return $this->modules;
    }
    
    /**-----------------------------------------------------------------------------------
     * @desc get module directory name
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *-----------------------------------------------------------------------------------*/
    public function getModuleDir() {
        $_appConfig = Vlib_Config::getConfig();
        $_env = APPLICATION_ENV ? APPLICATION_ENV : 'production';
        return $_appConfig[$_env]['resources']['frontController']['moduleDirectory'];
    }

    /**-----------------------------------------------------------------------------------
     * @desc get all the module name
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *-----------------------------------------------------------------------------------*/
    
    protected function _getModules() {
        return Vlib_IO_File::getDirs(APPLICATION_PATH . DS . 'modules');
    }

    /**-----------------------------------------------------------------------------------
     * @desc get the module route config files
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *------------------------------------------------------------------------------------*/
    
    protected function _getRouteConfig($_module) {
        $_configDir = APPLICATION_PATH . DS . 'modules' . DS . $_module . DS . 'config' . DS . 'routes';
        if (!is_dir($_configDir))
            return array();
        $_iterators = new DirectoryIterator($_configDir);
        $_configFiles = array();
        foreach ($_iterators as $_file) {
            if ($_file->isDir() || $_file->isDot())
                continue;
            $_name = $_file->getFilename();
            if (preg_match('/^[^a-z]/i', $_name) || ('CVS' == $_name)
                    || ('.svn' == strtolower($_name))) {
                continue;
            }
            $_configFiles[] = $_configDir . DS . $_name;
        }
        return $_configFiles;
    }

    /**-----------------------------------------------------------------------------------
     * @desc get the module config of all module
     * 
     * @author Tuyendn<tuyendn@vnext.vn>
     * @since 2011/10/05
     *-----------------------------------------------------------------------------------*/
    
    public function getRoutes() {
        if (null == $this->modules)
            return;
        $_router = new Zend_Controller_Router_Rewrite();
        foreach ($this->modules as $_module) {
            $_configFiles = $this->_getRouteConfig($_module);
            if ($_configFiles) {
                foreach ($_configFiles as $_file) {
                    $_config = new Zend_Config_Ini($_file, 'routes');
                    $_router->addConfig($_config, 'routes');
                }
            }
        }
        return $_router;
    }
}
?>